package demo;

import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-07-19
 * Time: 18:12
 */
public class DeleteJDBC {

    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/ebook?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");

        //建立连接
        Connection connection = dataSource.getConnection();

        /**
         * select id from borrow_info order by id desc limit 1;
         * +----+
         * | id |
         * +----+
         * | 19 |
         * +----+
         * 1 row in set (0.00 sec)
         *
         * mysql> delete from borrow_info where id = 19;
         * Query OK, 1 row affected (0.00 sec)
         *
         * mysql> select * from borrow_info;
         */

        //构建sql语句
        String sql = "delete from borrow_info order by id desc limit 1;";
        PreparedStatement statement = connection.prepareStatement(sql);

        int n = statement.executeUpdate();

        System.out.println("n = "+ n);

        statement.close();
        connection.close();
    }


}
